package xin.yangshuai.activemqgeneral.jms.broker;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * JmsProducer
 *
 * @author shuai
 * @date 2020/6/24
 */
public class JmsProducer {

    public static final String ACTIVEMQ_URL = "tcp://localhost:61616";
    public static final String QUEUE_NAME = "queue01";

    public static void main(String[] args) throws JMSException, InterruptedException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageProducer messageProducer = session.createProducer(session.createQueue(QUEUE_NAME));

        //开启持久化（队列默认的传送模式，保存未被消费的消息）
        //messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

        for (int i = 0; i < 30; i++) {
            //textMessage
            TextMessage textMessage = session.createTextMessage("msg---" + (i + 1));

            textMessage.setStringProperty("member", "vip");

            messageProducer.send(textMessage);
        }

        messageProducer.close();
        session.close();
        connection.close();

        System.out.println("*****消息发布到MQ完成");

    }
}
